
  let _loadImgNodes;
  let Lazyload = function (params) {
    this.errorImage = params.errorImage || false;
    this.loadImage = params.loadImage
    this.dataSelector = params.dataSelector ? params.dataSelector : 'data-src'
    this._init(params);
  };
  Lazyload.prototype = {
    _init: function (params) {
      var self = this;
      const selector = `[${this.dataSelector}]`
      _loadImgNodes = document.querySelectorAll(selector);
      self._judgeImages();
      window.addEventListener('scroll', function () {
        _loadImgNodes = document.querySelectorAll(selector);
        self._judgeImages();
      }, false);
    },
    _judgeImages: function () {
      var self = this;
      if (_loadImgNodes.length) {
        for (var i = 0; i < _loadImgNodes.length; i++) {
          if (_loadImgNodes[i].getBoundingClientRect().top < window.innerHeight) {
            self._loadImage(_loadImgNodes[i]);
          }
        }
      }
    },
    _loadImage: function (el) {
      if (this.loadImage) {
        this.loadImage(el)
        el.removeAttribute(this.dataSelector)
        return
      }
      var self = this;
      var img = new Image();
      img.src = el.getAttribute(this.dataSelector)
      el.src = el.getAttribute(this.dataSelector);
      el.removeAttribute(this.dataSelector);
      // // 图片加载失败
      img.onerror = function () {
        el.src = self.errorImage || el.getAttribute('src');
        el.removeAttribute(this.dataSelector);
      };
    }
  }
  

export default (options)=>{
	return new Lazyload(options)
}
